﻿//-----------------------------------------------------------------------
// <copyright file="EntityUtility.cs" company="Nathan Miller">
// Copyright (c) Nathan Miller.
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </copyright>
//-----------------------------------------------------------------------
namespace TileTool.Scene.Entity
{
    using System;
    using TileTool.Common.Math;

    /// <summary>
    /// Utility functions for working with entities.
    /// </summary>
    public static class EntityUtility
    {
        #region Constants - Public

        /// <summary>
        /// Min allowable size of an entity.
        /// </summary>
        public const float MinSize = 4.0f;

        /// <summary>
        /// Max allowable size of an entity.
        /// </summary>
        public const float MaxSize = 1024.0f;

        /// <summary>
        /// Default entity size.
        /// </summary>
        public const float DefaultSize = 0.5f;

        #endregion

        #region Methods - Public

        /// <summary>
        /// Gets the center point of the collection by taking the average
        /// of the center points of all entities in the collection.
        /// </summary>
        /// <param name="collection">Collection to get the center of.</param>
        /// <returns>Returns the center of the collection.</returns>
        public static Vector2D GetCenter(IEntityCollection collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection", "Collection must be non-null.");
            }

            if (collection.Count == 0)
            {
                return Vector2D.Zero;
            }

            Vector2D sum = Vector2D.Zero;

            foreach (IEntity entity in collection)
            {
                sum += entity.GetCenter();
            }

            sum *= 1.0f / collection.Count;

            return sum;
        }

        /// <summary>
        /// Gets the bounding rect the collection in world space.
        /// </summary>
        /// <param name="collection">Collection to get the center of.</param>
        /// <returns>Returns the bounding rectangle of the collection.</returns>
        public static Rect2D GetBoundingRect(IEntityCollection collection)
        {
            if (collection == null)
            {
                throw new ArgumentNullException("collection", "Collection must be non-null.");
            }

            if (collection.Count == 0)
            {
                return new Rect2D(Vector2D.Zero, 0.0f, 0.0f);
            }

            MinMax<float> boundsX = new MinMax<float>(float.MaxValue, float.MinValue);
            MinMax<float> boundsY = new MinMax<float>(float.MaxValue, float.MinValue);

            foreach (IEntity entity in collection)
            {
                Vector2D[] pts = entity.GetRectanglePoints();
                foreach (Vector2D pt in pts)
                {
                    boundsX.Add(pt.x);
                    boundsY.Add(pt.y);
                }
            }

            Rect2D res = new Rect2D();

            res.Pos = new Vector2D(boundsX.Min, boundsY.Min);
            res.Width = boundsX.Max - boundsX.Min;
            res.Height = boundsY.Max - boundsY.Min;

            return res;
        }

        #endregion
    }
}
